// 使用 D8 内置的 readbuffer 函数读取 wasm 文件
const buffer = readbuffer("/home/sjk/桌面/asc_test/build/test3.wasm");

// 初始化和加载 WebAssembly 模块
async function instantiateWasm(buffer) {
  const importObject = {
    env: {
      abort: function (message, fileName, lineNumber, columnNumber) {
        throw new Error(`Abort: ${message} in ${fileName}:${lineNumber}:${columnNumber}`);
      },
    },
  };

  const module = await WebAssembly.compile(buffer);
  const instance = await WebAssembly.instantiate(module, importObject);

  const memory = instance.exports.memory;

  // 获取最新的 DataView 对象
  function getDataView() {
    return new DataView(memory.buffer);
  }

  // 提取 WebAssembly 数组到 JavaScript 数组
  function parseArray(pointer, length) {
    const dataView = getDataView();
    const array = [];
    for (let i = 0; i < length; i++) {
      const objPointer = dataView.getUint32(pointer + i * 4, true);
      const id = dataView.getInt32(objPointer, true);
      array.push({ id });
    }
    return array;
  }

  // 将 JavaScript 数组转换为 WebAssembly 线性内存中的格式
  function lowerArray(values) {
    const dataView = getDataView();
    const length = values.length;
    const pointer = instance.exports.__pin(instance.exports.__new(length * 4, 1));
    for (let i = 0; i < length; i++) {
      dataView.setInt32(pointer + (i * 4), values[i], true);
    }
    instance.exports.__unpin(pointer);
    return pointer;
  }

  return {
    noInputWithOutputObject: instance.exports.noInputWithOutputObject,
    withInputAndOutputObject: instance.exports.withInputAndOutputObject,
    withInputNoOutputObject: instance.exports.withInputNoOutputObject,
    baselineTestObject: instance.exports.baselineTestObject,
    parseArray,
    lowerArray,
    memory,
  };
}

// 测试函数
async function test() {
  try {
    const wasmFunctions = await instantiateWasm(buffer);
    const iterations = 100;
    //wasmFunctions.baselineTestObject(); // 初始化

    // 辅助函数：执行测试并记录时间
    function runTest(name, testFunction, length) {
      const start = performance.now();
      for (let i = 0; i < iterations; i++) {
        const result = testFunction();

      }
      const end = performance.now();
      print(`${name}: ${end - start}ms`);
    }

    // 测试 baselineTestObject
    //runTest('Baseline Test', wasmFunctions.baselineTestObject, 0);

    // 测试 noInputWithOutputObject
    //runTest('No Input, With Output', () => wasmFunctions.noInputWithOutputObject(), 1000);

    // 测试 withInputAndOutputObject
    const inputArray3 = new Array(1000).fill(0).map((_, i) => i + 1);
    runTest('With Input, With Output', () => {      const inputPointer3 = wasmFunctions.lowerArray(inputArray3);      return wasmFunctions.withInputAndOutputObject(inputPointer3);}, 1000);

    // 测试 withInputNoOutputObject
    const inputArray4 = new Array(1000).fill(0).map((_, i) => i + 1);    
    //runTest('With Input, No Output', () => {      const inputPointer4 = wasmFunctions.lowerArray(inputArray4);      wasmFunctions.withInputNoOutputObject(inputPointer4);    }, 0);
  } catch (error) {
    print(`Error: ${error.message}`);
  }
}

// 运行测试
test().catch((err) => print(`Error: ${err}`));